package org.colafries.metadata.query.impl;

import org.colafries.metadata.query.DBQuery;
import org.colafries.common.enums.Flag;
import org.springframework.stereotype.Service;

/**
 * @author colafries
 * @version v1.0
 * @ClassName MysqlDBQuery
 * @Description
 * @date Created in 2024/3/20
 **/
@Service(value = "MysqlQuery")
public class MysqlQuery implements DBQuery {
    /**
     * 所有数据库信息查询 SQL
     */
    @Override
    public String schemaAllSql() {
        return "show databases";
    }

    /**
     * 数据库、模式、组织名称
     */
    @Override
    public String schemaName() {
        return "Database";
    }

    /**
     * 表信息查询 SQL
     */
    @Override
    public String tablesSql(String schemaName) {
        return "select TABLE_NAME AS `NAME`,TABLE_SCHEMA AS `Database`,TABLE_COMMENT AS COMMENT,TABLE_CATALOG AS `CATALOG`"
                + ",TABLE_TYPE AS `TYPE`,ENGINE AS `ENGINE`,CREATE_OPTIONS AS `OPTIONS`,TABLE_ROWS AS `ROWS`"
                + ",CREATE_TIME,UPDATE_TIME from information_schema.tables"
                + " where TABLE_SCHEMA = '" + schemaName + "'";
    }

    /**
     * 表字段信息查询 SQL
     */
    @Override
    public String columnsSql(String schemaName, String tableName) {
        return "select COLUMN_NAME,COLUMN_TYPE,COLUMN_COMMENT,COLUMN_KEY,EXTRA AS AUTO_INCREMENT"
                + ",COLUMN_DEFAULT,IS_NULLABLE,NUMERIC_PRECISION,NUMERIC_SCALE,CHARACTER_SET_NAME"
                + ",COLLATION_NAME,ORDINAL_POSITION from INFORMATION_SCHEMA.COLUMNS "
                + "where TABLE_SCHEMA = '" + schemaName + "' and TABLE_NAME = '" + tableName + "' "
                + "order by ORDINAL_POSITION";
    }
    /**
     * 表名称
     */
    @Override
    public String tableName() {
        return "NAME";
    }

    @Override
    public String columnName() {
        return "COLUMN_NAME";
    }

    /***
     * 字段类型
     * @return java.lang.String
     * @author colafries
     * @Date 2024/3/25
     */
    @Override
    public String columnType() {
        return "COLUMN_TYPE";
    }

    /**
     * 精度
     *
     * @return java.lang.String
     * @author colafries
     * @Date 2024/3/25
     */
    @Override
    public String precision() {
        return "NUMERIC_PRECISION";
    }

    /**
     * 字段注释
     *
     * @return
     * @author colafries
     * @Date 2024/3/25
     */
    @Override
    public String columnComment() {
        return "COLUMN_COMMENT";
    }

    /**
     * 主键字段
     *
     * @return java.lang.String
     * @author colafries
     * @Date 2024/4/3
     */
    @Override
    public String columnKey() {
        return "COLUMN_KEY";
    }
    /**
     * 主键值
     * @return java.lang.String
     * @author colafries
     * @Date 2024/4/3
     */
    public String isPK() {
        return "PRI";
    }

    /**
     * 字段长度
     *
     * @return java.lang.String
     * @author colafries
     * @Date 2024/4/3
     */
    @Override
    public String columnLength() {
        return "LENGTH";
    }

    /**
     * 是否允许为 NULL
     *
     * @return java.lang.String
     * @author colafries
     * @Date 2024/4/3
     */
    @Override
    public String isNullable() {
        return "IS_NULLABLE";
    }

    @Override
    public String nullableValue() {
        return Flag.YES.toString();
    }

    /**
     * @return 字符集名称
     */
    @Override
    public String characterSet() {
        return "CHARACTER_SET_NAME";
    }

    /**
     * @return 排序规则
     */
    @Override
    public String collation() {
        return "COLLATION_NAME";
    }

    /**
     * 字段序号
     */
    @Override
    public String columnPosition() {
        return "ORDINAL_POSITION";
    }

    /**
     * @return 小数范围
     */
    @Override
    public String scale() {
        return "NUMERIC_SCALE";
    }

    /**
     * 默认值
     */
    @Override
    public String defaultValue() {
        return "COLUMN_DEFAULT";
    }

    /**
     * 主键自增
     */
    @Override
    public String autoIncrement() {
        return "AUTO_INCREMENT";
    }
}
